package me.zhengjie.hr.employeebase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.hr.employeebase.domain.EmployeeBase;
import me.zhengjie.hr.employeebase.repository.EmployeeBaseRepository;
import me.zhengjie.hr.employeebase.service.EmployeeBaseService;
import me.zhengjie.hr.employeebase.service.dto.EmployeeBaseQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.sql.Timestamp;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ZGZ
 * @date 2020-04-15
 */
@Api(tags = "人员基本信息管理")
@RestController
@RequestMapping("/api/employeeBase2")
public class EmployeeBaseController2 {


    private final EmployeeBaseService employeeBaseService;
    @Autowired
    private EmployeeBaseRepository employeeBaseRepository;
    public EmployeeBaseController2(EmployeeBaseService employeeBaseService) {
        this.employeeBaseService = employeeBaseService;
    }



    @GetMapping
    @Log("查询人员基本信息")
    @ApiOperation("查询人员基本信息")
    @PreAuthorize("@el.check('employeeBase:list')")
    public ResponseEntity<Object> getEmployeeBaseszaizhiall(EmployeeBaseQueryCriteria criteria, Pageable pageable){
        System.out.println("-----2020-5-2--fwxx---zaizhi");
        return new ResponseEntity<>(employeeBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/zaizhi")
    @Log("查询人员基本信息")
    @ApiOperation("查询人员基本信息")
    @PreAuthorize("@el.check('employeeBase:list')")
    public ResponseEntity<Object> getEmployeeBaseszaizhi(Pageable pageable){
        System.out.println("-----2020-5-2--fwxx---zaizhi");

        return new ResponseEntity<>(employeeBaseRepository.getOnjob(pageable),HttpStatus.OK);
    }

    @GetMapping(value = "/timerange")
    @Log("查询人员基本信息")
    @ApiOperation("查询人员基本信息")
    @PreAuthorize("@el.check('employeeBase:list')")
    public ResponseEntity<Object> getEmployeeBasestimeRange(String timeStart, String timeEnd,Pageable pageable){
        System.out.println("-----2020-5-2--fwxx---zaizhitimeEnd"+timeEnd);
        System.out.println("-----2020-5-2--fwxx---zaizhitimeStart"+timeStart);
        String start  = timeStart.substring(0,10);
        String end  = timeEnd.substring(0,10);
//        System.out.println("-----2020-5-2--fwxx---ssssssss"+s);
        return new ResponseEntity<>(employeeBaseRepository.getTimeRange(start,end,pageable),HttpStatus.OK);

    }


    @GetMapping(value = "/lizhi")
    @Log("查询人员基本信息")
    @ApiOperation("查询人员基本信息")
    @PreAuthorize("@el.check('employeeBase:list')")
    public ResponseEntity<Object> getEmployeeBaseslizhi(Pageable pageable){
        System.out.println("-----2020-5-2--fwxx---lizhi");
        return new ResponseEntity<>(employeeBaseRepository.getLeave(pageable),HttpStatus.OK);
    }

    @PutMapping(value = "/leavesub")
    @Log("修改人员基本信息")
    @ApiOperation("修改人员基本信息")
    @PreAuthorize("@el.check('employeeBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EmployeeBase resources) {
        System.out.println("-----2020-5-2--fwxx---leavesub");
        if (resources.getLeaveTime() == null) {
            employeeBaseRepository.updateOnjob(resources.getId());
        }
        else { employeeBaseService.update(resources); }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

//    @PutMapping(value = "/lizhi-update")
//    @Log("修改人员基本信息")
//    @ApiOperation("修改人员基本信息")
//    @PreAuthorize("@el.check('employeeBase:edit')")
//    public ResponseEntity<Object> update(@Validated @RequestBody EmployeeBase resources) {
//        if (resources.get_leaveTime() == null) {
//            employeeBaseRepository.updateOnjob(resources.get_Id());
//        }
//        else {
//            employeeBaseService.update(resources);
//        }
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }


}

